package youleche.com.controler.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;

import youleche.com.business.BHistoryInfo;
import youleche.com.business.BShare;
import youleche.com.business.BVehicleImageInfo;
import youleche.com.business.BZzqy;
import youleche.com.common.IDFactory;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.interfaces.LoginAction;
import youleche.com.model.MHistoryInfo;
import youleche.com.model.MShare;
import youleche.com.model.MVehicleImageInfo;
import youleche.com.model.MZzqy;
import youleche.com.util.DateUtil;
import youleche.com.util.EditorUtil;
import youleche.com.util.FileUtil;
import youleche.com.util.JsonHelper;
import youleche.com.util.StaticUtil;

/**
 * Servlet implementation class SZzqyServlet
 */
public class SZzqyServlet extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * freemark静态化配置类
	 */
	 private Configuration cfg = null;
	 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SZzqyServlet() {
        super();
    }

    @Override
    public void init() throws ServletException
    {
       cfg = new Configuration();
       cfg.setServletContextForTemplateLoading(this.getServletContext(), null);
    }
    
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0:
				//新增
				this.addZzqyInfo(request, response);
				break;
		}
	}
	
	/**
	 * 新增
	 * @param request 请求
	 * @param response 响应
	 */
	private void addZzqyInfo(HttpServletRequest request, HttpServletResponse response){
		LoginAction loginHandler = new LoginAction();
		if(!loginHandler.is_Login(request, response)){ //未登录，请登录
			super.response(response, "2");
			return;
		}
		
		String userName = loginHandler.getLoginName(request, response);
		if("".equals(userName)){ //用户名为空，请登录
			super.response(response, "2");
			return;
		}
		
		MZzqy zzqyInfo = (MZzqy) JsonHelper.getObjectByJsonStringAndGson(request, MZzqy.class);
		zzqyInfo.setCallName(userName);
		zzqyInfo.setAddtime(DateUtil.getNowDate());
		zzqyInfo.setId(IDFactory.getId("mlc", "zzqy"));
		List<String> imgList = EditorUtil.getImagePathListByContent(zzqyInfo.getContent());
		for (String imgPath : imgList) {
			zzqyInfo.setContent(zzqyInfo.getContent().replace("uploadTemp/" + imgPath, "upload/" + imgPath));
		}
		
		
		BZzqy handler = new BZzqy();
		try {
			if(handler.addZzqyInfo(zzqyInfo) >0){
				this.handleImageByContent(imgList);
				this.batchAddImage(imgList, zzqyInfo);
				StaticUtil.staticZzqy(zzqyInfo, cfg, request, response);	
				super.response(response, "1");
				return;
			}
		} catch (Exception e) {
			super.response(response, "0");
			e.printStackTrace();
		}
		
		super.response(response, "0");
	}
	
	/**
	 * 批量添加图片进入图片库
	 * @param imgPathList 图片列表
	 * @param share 组织信息
	 */
	private void batchAddImage(List<String> imgPathList, MZzqy zzqy){
		BVehicleImageInfo imageHandler = new BVehicleImageInfo();
		MVehicleImageInfo imageInfo = null;
		for (String path : imgPathList) {
			imageInfo = new MVehicleImageInfo();
			imageInfo.setImagePath(path);
			imageInfo.setImageType("6"); //6代表组织骑游
			imageInfo.setSellerId(zzqy.getCallName());
			imageInfo.setVehicleId(zzqy.getId());
			imageInfo.setAddTime(DateUtil.getNowDate());
			imageInfo.setImageId(IDFactory.getId("mlc", "zzqy"));
			try {
				imageHandler.addVehicleImageInfo(imageInfo);
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
		}
	}
	
	/**
	 * 根据内容处理图片信息
	 * @param imgPathList 图片id以及路径
	 */
	private void handleImageByContent(List<String> imgPathList){
		//批量复制图片到正式库
		//批量删除临时库图片
		String tempDir = this.getServletConfig().getServletContext().getRealPath("") + "/uploadTemp/"; //临时库
		String normalDir = this.getServletConfig().getServletContext().getRealPath("") + "/upload/"; //正式库
		for (String path : imgPathList) {
			FileUtil.copyFile(tempDir + path, normalDir + path);
			FileUtil.deleteFile(path, tempDir);
		}
	}

}
